package com.link.base.tencent.corpwx.appchat.service;

import com.link.base.tencent.corpwx.appchat.dao.mybatis.mapper.AppChatUserMapper;
import com.link.base.tencent.corpwx.appchat.model.AppChatUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.rowset.serial.SerialException;
import java.util.List;

/**
 * 应用群聊-成员 
 *
 * @author 欧阳振强
 * @date 2020-01-03 10:41:04.0
 */
@Service
public class AppChatUserServiceImpl extends BasicServiceImpl<AppChatUser> implements AppChatUserService {
    /**群聊状态-等待加入*/
    private static final String CHAT_STATUS_WAIT_ADD = "waitAdd";
    /**群聊状态-已加入*/
    private static final String CHAT_STATUS_ALREADY_ADD = "alreadyAdd";
    /**群聊状态-等待删除*/
    private static final String CHAT_STATUS_WAIT_DEL = "waitDel";
    /**群聊状态-已删除*/
    private static final String CHAT_STATUS_ALREADY_DEL = "alreadyDel";

    @Resource
    private AppChatUserMapper appChatUserMapper;
    
    @Override
    public BasicMapper<AppChatUser> getBasicMapper() {
        return appChatUserMapper;
    }

    @Override
    public void addAppChatUser(AppChatUser record) throws Exception {
        String chatStatus = record.getChatStatus();
        if (CHAT_STATUS_ALREADY_DEL.equals(chatStatus)) {
            // 已删除状态成员，则更新为等待加入
            record.setChatStatus(CHAT_STATUS_WAIT_ADD);
            appChatUserMapper.update(record);
        } else if (CHAT_STATUS_WAIT_DEL.equals(chatStatus)) {
            // 等待删除状态成员，则直接更为已加入
            record.setChatStatus(CHAT_STATUS_ALREADY_ADD);
            appChatUserMapper.update(record);
        } else {
            throw new SerialException("非法状态成员！");
        }
    }

    @Override
    public void delAppChatUser(AppChatUser record) throws Exception {
        String chatStatus = record.getChatStatus();
        if (CHAT_STATUS_ALREADY_ADD.equals(chatStatus)) {
            // 已加入状态成员，则更新为等待删除
            record.setChatStatus(CHAT_STATUS_WAIT_DEL);
            appChatUserMapper.update(record);
        } else if (CHAT_STATUS_WAIT_ADD.equals(chatStatus)) {
            // 等待加入状态成员，则直接更新为已删除
            record.setChatStatus(CHAT_STATUS_ALREADY_DEL);
            appChatUserMapper.update(record);
        } else {
            throw new SerialException("非法状态成员！");
        }
    }

    @Override
    public List<AppChatUser> queryUnSelectUserListPage(AppChatUser appChatUser) throws Exception {
        return appChatUserMapper.queryUnSelectUserListPage(appChatUser);
    }
}